import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_axrjc/datasource/network/bean/EventBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/MainPageEnum.dart';
import 'package:flutter_axrjc/datasource/network/bean/PageIndexBean.dart';
import 'package:flutter_axrjc/page/main/check_manage/view.dart';
import 'package:flutter_axrjc/page/main/knowledge/view.dart';
import 'package:flutter_axrjc/page/main/main_home/logic.dart';
import 'package:flutter_axrjc/page/main/main_home/state.dart';
import 'package:flutter_axrjc/page/main/main_home/view.dart';
import 'package:flutter_axrjc/page/main/mine/view.dart';
import 'package:flutter_axrjc/util/JPushUtil.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../main.dart';
import 'state.dart';

class MainLogic extends GetxController {
  final MainState state = MainState();
  var pageController = PageController(initialPage: 0);

  StreamSubscription<EventBean>? listener;
  @override
  void onInit() {
    super.onInit();
    Util.setUi();
    state.pages.addAll([
      PageIndexBean(
          name: MainPageEnum.home.name,
          page: const MainHomePage(),
          bottom: BottomNavigationBarItem(
            label: MainPageEnum.home.label,
            icon: Obx(() {
              return Image.asset(
                state.currentIndex.value == 0
                    ? "assets/main/home_selected.png"
                    : "assets/main/home.png",
                scale: 3,
              );
            }),
          )),
      PageIndexBean(
          name: MainPageEnum.check.name,
          page: const CheckManagePage(),
          bottom: BottomNavigationBarItem(
            label: MainPageEnum.check.label,
            icon: Obx(() {
              return Image.asset(
                state.currentIndex.value == 1
                    ? "assets/main/check_selected.png"
                    : "assets/main/check.png",
                scale: 3,
              );
            }),
          )),
      PageIndexBean(
          name: MainPageEnum.knowledge.name,
          page: const KnowledgePage(),
          bottom: BottomNavigationBarItem(
            label: MainPageEnum.knowledge.label,
            icon: Obx(() {
              return Image.asset(
                state.currentIndex.value == 2
                    ? "assets/main/knowledge_selected.png"
                    : "assets/main/knowledge.png",
                scale: 3,
              );
            }),
          )),
      PageIndexBean(
          name: MainPageEnum.mine.name,
          page: const MinePage(),
          bottom: BottomNavigationBarItem(
            label: MainPageEnum.mine.label,
            icon: Obx(() {
              return Image.asset(
                state.currentIndex.value == 3
                    ? "assets/main/mine_selected.png"
                    : "assets/main/mine.png",
                scale: 3,
              );
            }),
          ))
    ]);
    requestPermission();
    listener = eventBus.on<EventBean>().listen((event) {
      if (event.type == EventType.changeMainPage) {
        changePageTo(event.data as int);
      }
    });
  }

  Future requestPermission() async {
    await [Permission.storage].request();
    initPushAndPermission();
  }

  void changePageTo(int i) {
    var logic = Get.find<MainHomeLogic>();
    if (state.currentIndex.value == 0 &&
        logic.state.checkStatus.value == CheckStatusType.checking) {
      EasyLoading.showToast("正在进行灯光检测");
      return;
    }
    state.currentIndex.value = i;
    pageController.animateToPage(i,
        duration: const Duration(milliseconds: 200), curve: Curves.linear);
  }

  @override
  void onClose() {
    super.onClose();
    listener?.cancel();
  }
}
